package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.dto.VarietyDto;
import com.wizz.trade.information.dto.page.VarietyPageDto;
import com.wizz.trade.information.entity.Variety;
import com.wizz.trade.information.service.VarietyService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.vo.VarietyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资料模块/商品品种
 */
@RestController
@RequestMapping("/variety")
public class VarietyController {

    @Resource
    private VarietyService varietyService;

    /**
     * 添加操作
     *
     * @param varietyDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult addVariety(@RequestBody VarietyDto varietyDto) {
        Variety variety = new Variety();
        BeanUtils.copyProperties(varietyDto, variety);
        varietyService.addVariety(variety);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新（编辑）操作
     *
     * @param varietyDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult updateVariety(@RequestBody VarietyDto varietyDto, @PathVariable("id") Integer id) {
        Variety variety = new Variety();
        BeanUtils.copyProperties(varietyDto, variety);
        variety.setId(id);
        varietyService.updateVariety(variety);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }


    /**
     * 删除（撤销）操作
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult deleteVariety(@PathVariable("id") Integer id) {
        varietyService.deleteVariety(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     *
     * @param varietyPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult<PageVo<VarietyVo>> list(VarietyPageDto varietyPageDto) {
        PageVo<Variety> rawPage = varietyService.getPage(varietyPageDto);
        List<Variety> data = rawPage.getData();
        List<VarietyVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            VarietyVo varietyVo = new VarietyVo();
            BeanUtils.copyProperties(data.get(i), varietyVo);
            dataVo.add(varietyVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取品种下拉框
     * @return 选项
     */
    @GetMapping("/options")
    public ResponseResult<List<Map<Integer, String>>> getOptions(){
        return new ResponseResult<>(StatusCode.OK, varietyService.getOptions());
    }


}
